package com.ruoyi.web.service.devicedata.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.web.domain.device.DeviceInfo;
import com.ruoyi.web.domain.devicedata.DevicePhoto;
import com.ruoyi.web.domain.insect.InsectRecord;
import com.ruoyi.web.mapper.devicedata.DevicePhotoMapper;
import com.ruoyi.web.mapper.insect.InsectRecordMapper;
import com.ruoyi.web.pojo.request.devicedata.DevicePhotoRequest;
import com.ruoyi.web.pojo.vo.devicedata.DevicePhotoVO;
import com.ruoyi.web.pojo.vo.devicedata.InsectNamePhotoVO;
import com.ruoyi.web.service.device.DeviceInfoService;
import com.ruoyi.web.service.devicedata.DevicePhotoService;
import com.ruoyi.web.service.insect.InsectRecordService;
import com.ruoyi.web.utils.wuqing.WQRremoteCall;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 设备图片表 服务实现类
 * @author fwx
 * @date 2024-01-24 10:46:59
 */
@Service
@Slf4j
@DataSource(value = DataSourceType.SLAVE)
public class DevicePhotoServiceImpl extends ServiceImpl<DevicePhotoMapper, DevicePhoto> implements DevicePhotoService {

    @Autowired
    private InsectRecordMapper insectRecordMapper;

    @Autowired
    private InsectRecordService insectRecordService;

    @Autowired
    private DevicePhotoMapper devicePhotoMapper;

    @Autowired
    private DeviceInfoService deviceInfoService;


    @Override
    public TableDataInfo<DevicePhotoVO> queryPhotoPage(DevicePhotoRequest request){
        return WQRremoteCall.queryPhotoPage(request);
    }


    @Override
    public List<DevicePhotoVO> getByInsectName(String deviceId, String time, String insectName){
        return WQRremoteCall.getByInsectName(deviceId, time, insectName);
    }

    @Override
    public List<DevicePhotoVO> getByPhotos(List<Long> photoIds,String deviceId){
        List<InsectRecord> records = insectRecordService.getByPhotoIds(photoIds);
        List<DevicePhoto> photos = devicePhotoMapper.getByPhotoIds(photoIds,deviceId);
        Map<Long, List<InsectRecord>> recordMap = records.stream().collect(Collectors.groupingBy(InsectRecord::getPhotoId));
        List<DevicePhotoVO> result = new ArrayList<>();

        DeviceInfo device = deviceInfoService.getByDeviceId(deviceId);

        for (DevicePhoto photo : photos) {
            DevicePhotoVO vo = new DevicePhotoVO(photo,recordMap.get(photo.getAttachmentId()));
            vo.setDeviceName(device.getDeviceName());
            result.add(vo);
        }

        result = result.stream().sorted(Comparator.comparing(DevicePhotoVO::getPicTime).reversed()).collect(Collectors.toList());

        return result;
    }

}
